package com.gin.kotlin.no_gay_alliance.adapter

import android.content.Context
import android.content.DialogInterface
import android.view.View
import com.gin.kotlin.no_gay_alliance.R
import com.gin.kotlin.no_gay_alliance.base.AbstractBindingRecyclerViewAdapter
import com.gin.kotlin.no_gay_alliance.base.AlertDialogUtils
import com.gin.kotlin.no_gay_alliance.base.DownloadUtils
import com.gin.kotlin.no_gay_alliance.base.MyHandler
import com.gin.kotlin.no_gay_alliance.databinding.ItemAppReleaseBinding
import com.gin.kotlin.no_gay_alliance.model.AppReleaseModel
import com.gin.kotlin.no_gay_alliance.utils.TimeUtils.Companion.zdtIntoTextView

class AppReleaseAdapter(context: Context, handler: MyHandler) : AbstractBindingRecyclerViewAdapter<ItemAppReleaseBinding, AppReleaseModel>(context, handler), View.OnClickListener {
    override fun onBindViewHolder(holder: ViewHolder<ItemAppReleaseBinding>, position: Int, b: ItemAppReleaseBinding, dataModel: AppReleaseModel) {
        b.tvTagName.text = dataModel.tagName
        b.tvBody.text = dataModel.body
        b.tvFlagPreRelease.visibility = if (dataModel.preRelease == true) View.VISIBLE else View.GONE

        dataModel.createdAt?.also { zdtIntoTextView(context, it, b.tvTimestamp, getAttrColor(R.attr.textColor)) }

        b.root.tag = dataModel
        b.root.setOnClickListener(this)
    }

    override fun onClick(v: View?) {
        val appRelease = v?.tag as? AppReleaseModel ?: return
        AlertDialogUtils.builder(context,"下载版本: ${appRelease.tagName}", listener = { _, which ->
            if (which==DialogInterface.BUTTON_POSITIVE){
                DownloadUtils.enqueue(context, listOf(DownloadUtils.DownloadOption(appRelease.obtainUrl()!!, appRelease.obtainFilename()!!)))
                handler.toast("开始下载")
            }
        }).show()
    }
}